let db = require('../db/index')

//获取todolist表中未被删除的事项
exports.all = (req, res) => {        
    var sql = 'select * from todolist where isDeleted=0'
    db.query(sql, (err, data) => {
        if(err) {
            return res.send('错误：' + err.message)
        }
        res.send(data)
    })
}

//通过id删除事项
exports.del = (req, res) => {        
    var sql = 'update todolist set isDeleted=1 where id = ?'
    db.query(sql, [req.query.id], (err, data) => {
        if(err) {
            return res.send('错误：' + err.message)
        }
        if(data.affectedRows > 0) {
            res.send({
              status: 200,
              message: '删除成功'
            })
        }else{
            res.send({
              status: 202,
              message: '删除失败'
            })
        }
    })
}

//向表中添加事项
exports.add = (req, res) => {        
    var sql = 'insert into todolist (content) values (?)'
    db.query(sql, [req.query.content], (err, data) => {
        if(err) {
            return res.send('错误：' + err.message)
        }
        if(data.affectedRows > 0) {
            res.send({
              status: 200,
              message: 'success'
            })
        }else{
            res.send({
              status: 202,
              message: 'error'
            })
        }
    })
}


//通过id更新isPicked值
exports.update = (req, res) => {        
    var sql = 'update todolist set isPicked=? where id = ?'
    db.query(sql, [req.query.isPicked, req.query.id], (err, data) => {
        if(err) {
            return res.send('错误：' + err.message)
        }
        if(data.changedRows > 0) {
          res.send({
            status: 200,
            message: 'success'
          })
        }else{
          res.send({
            status: 202,
            message: 'error'
          })
        }
    })
}
